package com.delos.rbac.security;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.AuthenticationProvider;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.stereotype.Component;
import org.springframework.util.ClassUtils;

/**
 * @author zouyaowen
 * @date 2020-05
 */
@Component
@Slf4j
public class TokenAuthenticationProvider implements AuthenticationProvider {

    @Autowired
    private TokenManager tokenManager;

    @Override
    public Authentication authenticate(Authentication authentication) throws AuthenticationException {
        TokenAuthentication tokenAuth = (TokenAuthentication) authentication;
        tokenManager.parseToken(tokenAuth);
        return tokenAuth;
    }

    @Override
    public boolean supports(Class<?> authentication) {
        return ClassUtils.isAssignable(TokenAuthentication.class, authentication);
    }
}
